#include "precom.h"
#include "Dialogs.h"

static Dialogs *diag;

void CALLBACK Dialogs::RadiusWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	diag->OnRadiusWndProc(hwnd, msg, wParam, lParam);
}

LRESULT Dialogs::OnRadiusWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	TCHAR buff[20];
	DWORD pos;
	switch(msg) {
		case WM_INITDIALOG:
			if(res != 0.0f) {
				//wpisz w pole textowe wartosc
				swprintf_s(buff, 20, L"%f", res);
				SetDlgItemText(hwnd, IDC_RADIUSEDIT, buff);
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
						TBM_SETLINESIZE, 0, 1);
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
					TBM_SETPAGESIZE, 0, 10);
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
					TBM_SETRANGEMIN, FALSE, 1);
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
					TBM_SETRANGEMAX, TRUE, 200);
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
					TBM_SETTICFREQ, 10, 0);
				pos = static_cast<DWORD>(floorf(res*100.0f));
				SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
						TBM_SETPOS, TRUE, pos);
			}			
			break;
		case WM_HSCROLL:
			switch(wParam) {				
				case TB_THUMBPOSITION:
				case TB_ENDTRACK:
					pos = SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
						TBM_GETPOS, 0, 0);
					res = (float)pos / 100.0f;
					swprintf_s(buff, 20, L"%f", res);
					SetDlgItemText(hwnd, IDC_RADIUSEDIT, buff);
					break;
			}
			break;
		case WM_COMMAND:
			if(HIWORD(wParam) == EN_CHANGE) {
				if(0 < GetDlgItemText(hwnd, IDC_RADIUSEDIT, buff, 20)) {
						res = static_cast<float>(_wtof(buff));
						pos = static_cast<DWORD>(floorf(res*100.0f));
						SendDlgItemMessage(hwnd, IDC_RADIUSSLIDER, 
							TBM_SETPOS, TRUE, pos);
					}
			}
			switch(wParam) {
				case IDOK:
					if(0 < GetDlgItemText(hwnd, IDC_RADIUSEDIT, buff, 20)) {
						res = static_cast<float>(_wtof(buff));
					}
					succes = true;
				case IDCANCEL:
					EndDialog(hwnd, 0);
					return TRUE;
			}
			break;
		default:
			return FALSE;
	}
	return TRUE;
}

Dialogs::Dialogs(HWND parent, HINSTANCE hInstance) {
	hWndParent = parent;
	hInst = hInstance;
	diag = this;
	res = 0.0f;
}

void Dialogs::Init(HWND parent, HINSTANCE hInstance) {
	hWndParent = parent;
	hInst = hInstance;
}

bool Dialogs::GetRadius(float a_prevRadius)
{
	succes = false;
	res = a_prevRadius;
	DialogBox(hInst, MAKEINTRESOURCE(IDD_RADIUSDIALOG),
		hWndParent, (DLGPROC)RadiusWndProc);
	return succes;
}